package jsu.ask.controller;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import jsu.ask.util.*;
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import jsu.ask.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

@Controller
@RequestMapping("/search")
public class SearchControll {
    @Autowired
    @Qualifier("SearchServiceImpl")
    private SearchService searchService;

    @RequestMapping(value = "/answer",method = RequestMethod.POST)
    public void answer(String quest, HttpServletResponse response) throws SQLException {
        DBUtil db=new DBUtil();
        ResultInfo resultInfo=new ResultInfo();
        List<String> keys=TextParse.Keys(quest);
        System.out.println(keys.toString());
        System.out.println(keys.size());
        if(keys.size()>1){
            ResultSet rs = db.rs("MATCH(n:" + keys.get(1) + ") WHERE n.name='" + keys.get(0) + "' RETURN n");
            //System.out.println(rs.toString());
            if (rs == null) {
                resultInfo.setCode(0);
                resultInfo.setMessage("这个问题太深奥了，以后再来问吧");
                System.out.println("没找到");
            } else {
                List<String> answers=new ArrayList<String>();
                if(rs.next()){
                    System.out.println("进来了");
                    resultInfo.setCode(1);
                    String jsonstring = rs.getString("n");
                    System.out.println(jsonstring);
                    JsonObject jsonObject = (JsonObject) new JsonParser().parse(jsonstring);
                    if(keys.size()==2){
                        Set<String> keySet = jsonObject.keySet();
                        for(String s:keySet)
                            answers.add(jsonObject.get(s).toString());
                    }
                    for(int i=2;i<keys.size();i++)
                    if(jsonObject.get(keys.get(i))!=null){
                        System.out.println(jsonObject.get(keys.get(i)));
                        answers.add(jsonObject.get(keys.get(i)).toString()+"\n");
                    }
                    else{
                        ResultSet rs2 = db.rs("MATCH (:"+keys.get(1)+"{name:'"+keys.get(0)+"'})-[:"+keys.get(i)+"]->(b) RETURN b");
                        while(rs2.next()){
                            String jsonstring2 = rs2.getString("b");
                            System.out.println(jsonstring2);
                            JsonObject jsonObject2 = (JsonObject) new JsonParser().parse(jsonstring2);
                            Set<String> keySet = jsonObject2.keySet();
                            for(String s:keySet)
                            answers.add(jsonObject2.get(s).toString());
                        }
                    }
                }
                String str= answers.toString().replace("\\n","<br/><br/>");
                System.out.println(str);
                resultInfo.setMessage(str);
            }
        }else{
            resultInfo.setMessage("这个问题太深奥了，以后再来问吧!!!");
        }
        db.close();
        JsonUtils.toJson(response, resultInfo);
    }
    @RequestMapping("/query")
    public String queryDate(String quest,Model model) throws SQLException {
        DBUtil db=new DBUtil();
        List<String> keys=TextParse.Keys(quest);
        System.out.println(keys.toString());
        System.out.println(keys.size());
        model.addAttribute("quest",quest);
        if(keys.size()>0){
            ResultSet rs = db.rs("MATCH(n:" + keys.get(1) + ") WHERE n.name='" + keys.get(0) + "' RETURN n");
            //System.out.println(rs.toString());
            if (rs == null) {
                System.out.println("搜索不到");
                model.addAttribute("send_msg","0");
            } else {
                if(rs.next()){
                    String jsonstring = rs.getString("n");
                    System.out.println(jsonstring);
                    jsonstring=jsonstring.replace("\\n","<br/><br/>");
                    JsonObject jsonObject = (JsonObject) new JsonParser().parse(jsonstring);
                    System.out.println(jsonObject.toString());
                    Map<String, String> hm = new HashMap<>();
                    Set<String> strings = jsonObject.keySet();
                    for(String s : strings){
                        hm.put(s,jsonObject.get(s).toString());
                    }
                    model.addAttribute("msg",hm);
                    model.addAttribute("send_msg","1");
                }
            }

        }else{
            System.out.println("没有关键字");
            model.addAttribute("send_msg","0");
        }
        db.close();
        return "search";
    }
}
